package org.examples;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import io.netty.handler.codec.http.HttpHeaderNames;
import lombok.extern.slf4j.Slf4j;
import reactor.netty.http.client.HttpClient;

import static org.examples.DebugConstants.gwPath;
import static org.examples.DebugConstants.gwRequestData;
import static org.examples.DebugConstants.gwTtsParameter;
import static org.examples.DebugConstants.host;
import static org.examples.DebugConstants.isHttps;

@Slf4j
public class ReactorGw {
  public static void main(final String[] args) throws Exception {
    log.info("ReactorGw.main");
    HttpClient client = HttpClient.create().baseUrl(host);
    if (isHttps()) {
      client = client.secure();
    }
    client
        .headers(headers -> {
          headers.set(HttpHeaderNames.ACCEPT, "audio/ogg;codecs=opus");
        })
        .post()               // Specifies that POST method will be used
        .uri(gwPath())   // Specifies the path
        .sendForm((req, form) -> {
          req.requestHeaders().remove(HttpHeaderNames.TRANSFER_ENCODING);
          form
              .multipart(true)
              .file("RequestData", new ByteArrayInputStream(gwRequestData.getBytes(StandardCharsets.UTF_8)), "application/json")
              .file("TtsParameter","paramName=\"TEXT_TO_READ\"", new ByteArrayInputStream(gwTtsParameter.getBytes(StandardCharsets.UTF_8)),
                  "application/json");
//              .attr("paramName","\"TEXT_TO_READ\"");
        })  // Sends the request body
        .responseContent()    // Receives the response body
        //                .aggregate()
        //                .asString()
        .log("http-client")
        //        .block();
        .blockLast();
  }
}
